<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'unitmanage:unitEnterpriseDominant:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.monomer }}
        </a>
      </template>
      <!-- 新增 action 插槽 -->
      <template #action="{ record }">
        <a-button @click="() => { currentRecord.value = reactive(record); handleFormDevice({ tid: record.tid }); }" v-auth="'unitmanage:equipmentSelection:edit'" type="text">
         <span style="font-size:14px; color: #CD5C5C;">火情展示设备配置 </span> 
        </a-button>
        <a-button @click="handleFormDetail({ tid: record.tid })" type="text">
          <span style="font-size: 14px; color: #4169E1;">详情</span>
        </a-button>
        <a-dropdown>
      <a-button type="text"><span style="color: #A0522D;">更多操作>></span> <a-icon type="down" /></a-button>
      <template #overlay>
        <a-menu>
          <a-menu-item @click="handleForm({ tid: record.tid })">
            <span style="font-size:14px; color: #52c41a;" v-auth="'unitmanage:unitEnterpriseDominant:edit'">编辑</span>
          </a-menu-item>
          <a-menu-item @click="handleFormBuilding({ unitCode: record.unitCode })">
            <span style="font-size:14px; color: #528B8B;">建筑信息</span>
          </a-menu-item>
          <a-menu-item @click="handleFormRegion({ unitCode: record.unitCode })">
            <span style="font-size:14px; color: #00BFFF;">区域信息</span>
          </a-menu-item>
          <a-menu-item>
            <a-popconfirm
              title="是否确认删除主体信息表"
              ok-text="确认"
              cancel-text="取消"
              @confirm="handleDelete(record)"
            >
              <template #icon>
                <a-icon type="question-circle-o" style="color: red;" />
              </template>
              <a @click="e => e.preventDefault()">
                <span style="color: red;">删除</span>
              </a>
            </a-popconfirm>
          </a-menu-item>
        </a-menu>
      </template>
    </a-dropdown>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormDeatil @register="registerDrawerDetail" @success="handleSuccess" />
    <FormDevice :record="currentRecord" @register="registerModalDevice" @success="handleSuccess" />
    <BuildingModal @register="registerModalBuilding" :unitCode="UnitCode" />
    <RegionModal @register="registerModalRegion" :unitCode="UnitCode" />
  </div>
</template>
<script lang="ts" setup name="ViewsUnitmanageUnitEnterpriseDominantList">
  import { unref, ref, onMounted, reactive, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { unitEnterpriseDominantDelete, unitEnterpriseDominantListData } from '/@/api/unitmanage/unitEnterpriseDominant';
  import { equipmentSelectionForm } from '/@/api/unitmanage/equipmentSelection';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormDeatil from './formDetail.vue';
  import FormDevice from './formDevice.vue';
  import BuildingModal from './buildingModal.vue';
  import RegionModal from './regionModal.vue';
  import { useDataFetching } from '/@/utils/Deduplicate';
  import { officeTreeData } from '/@/api/sys/office';
  import { Popconfirm } from 'ant-design-vue'; // 引入 Popconfirm 组件
  // 使用组合式函数
  const { uniqueArray, getAllData } = useDataFetching();
  const { t } = useI18n('unitmanage.unitEnterpriseDominant');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const UnitCode = ref('');

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('主体信息表管理'),
  };

  const EnterpriseName = ref('');
  const EnterpriseParams = computed(() => {
    return {
      name: EnterpriseName.value,
    };
  });

  const searchForm: FormProps = {
    baseColProps: { md: 8, lg: 6 },
    labelWidth: 90,
    schemas: [
      {
        label: t(''),
        field: 'name',
        // fieldLabel: 'name',
        component: 'Input',
        componentProps: {
          placeholder: t('请输入主体名称'),
          // api: unitEnterpriseDominantListData,
          // fieldNames: { label: 'name', value: 'name' },
          // resultField: 'list',
          // params: EnterpriseParams,
          // allowClear: true,
          // immediate: false,
          // each: false,
          // showSelect: true,
          // optionFilterProp: 'label',
          // showSearch: true,
          // onSearch: (value) => {
          //   EnterpriseName.value = value;
          //   // console.log('输入的值:', value);
          //   // reload()
          // },
        },
      },
      {
        label: t(''),
        field: 'dominantType',
        // fieldLabel:'dominantType',
        component: 'TreeSelect',
        componentProps: {
          placeholder: t('请输入主体类型'),
          maxlength: 100,
          dictType: 'dominant_type',
          allowClear: true,
        },
      },
      {
        label: t(''),
        field: 'mobile',
        component: 'Input',
        componentProps: {
          placeholder: t('管理员电话'),
          allowClear: true,
          autoSubmitOnEnter: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('主体名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    // {
    //   title: t('主体类型'),
    //   dataIndex: 'dominantType',
    //   key: 'a.dominant_type',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    {
      title: t('管理员'),
      dataIndex: 'contacts',
      key: 'a.contacts',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('管理员电话'),
      dataIndex: 'mobile',
      key: 'a.mobile',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('座机号码'),
      dataIndex: 'tel',
      key: 'a.tel',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('主体模式'),
      dataIndex: 'state',
      key: 'a.state',
      sorter: true,
      width: 130,
      align: 'left',
      dictType: 'enterprise_model',
    },
    {
      title: t('操作'),
      dataIndex: 'action',
      key: 'action',
      width: 400,
      fixed: 'right',
      slot: 'action', // 指定使用 action 插槽
    },
  ];

  const [registerTable, { reload }] = useTable({
    api: unitEnterpriseDominantListData,
    beforeFetch: (params) => {
      const urlParams = new URLSearchParams(window.location.search);
      const dominantType = urlParams.get('dominantType');
      if (dominantType) {
        params.dominantType = dominantType;
      }
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn, // 移除原有的 actionColumn 配置
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerDrawerDetail, { openDrawer: openDrawerDetail }] = useDrawer();
  const [registerModalDevice, { openModal: openModalDevice }] = useModal();
  const [registerModalBuilding, { openModal: openModalBuilding }] = useModal();
  const [registerModalRegion, { openModal: openModalRegion }] = useModal();
  // 定义一个响应式变量来存储当前的 record
  const currentRecord = ref<Recordable>({});

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleFormDetail(record: Recordable) {
    openDrawerDetail(true, record);
  }

  function handleFormDevice(record: Recordable) {
    openModalDevice(true, record);
  }

  function handleFormBuilding(record: Recordable) {
    UnitCode.value = record.unitCode;
    // console.log('UnitCode:',UnitCode.value);
    openModalBuilding(true, record);
  }

  function handleFormRegion(record: Recordable) {
    UnitCode.value = record.unitCode;
    // console.log('UnitCode:',UnitCode.value);
    openModalRegion(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { tid: record.tid };
    const res = await unitEnterpriseDominantDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

</script>